//
//  CellThreeView.m
//  JinChengYun
//
//  Created by mac on 2017/7/18.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "CellThreeView.h"

@implementation CellThreeView
- (id)initWithFrame:(CGRect)frame witharray:(NSMutableArray *)array{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataarray = [NSMutableArray arrayWithArray:array];

        [self creatviewwitharray:self.dataarray];

           }
    return self;
}
- (void)creatviewwitharray:(NSMutableArray *)array{
    _namelab = [[UILabel alloc] initWithFrame:CGRectMake(8, 15, 90, 18)];
    _namelab.text   = @"联系方式:";
    _namelab.textColor = [UIColor blackColor];
    _namelab.font = [UIFont systemFontOfSize:14];
    [self addSubview:_namelab];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(self.frame.size.width-30, 10, 30, 30);
    
    btn.layer.cornerRadius = 6;
    btn.backgroundColor = [UIColor whiteColor];
    [btn setImage:[UIImage imageNamed:@"Btn_jiahao"] forState:0];
    
    [btn addTarget:self action:@selector(gotoAddressBookBtnClick1:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 1;
    [self addSubview:btn];
    _rightbtn1 = btn;
    UITextField *tfi = [[UITextField alloc] initWithFrame:CGRectMake(105, 10, 180, 30)];
    tfi.backgroundColor = [UIColor whiteColor];
    tfi.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:tfi];
    _tf1 = tfi;
    _tf1.delegate = self;
    

    _rightbtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightbtn2.frame = CGRectMake(self.frame.size.width-30, 10+50, 30, 30);
    
    _rightbtn2.layer.cornerRadius = 6;
    _rightbtn2.backgroundColor = [UIColor whiteColor];
    [_rightbtn2 setImage:[UIImage imageNamed:@"Btn_jian"] forState:0];
    [_rightbtn2 addTarget:self action:@selector(BtnClick2:) forControlEvents:UIControlEventTouchUpInside];
    _rightbtn2.tag = 2;
    [self addSubview:_rightbtn2];
    _rightbtn2.userInteractionEnabled = YES;
    _tf2 = [[UITextField alloc] initWithFrame:CGRectMake(105, 10+50, 180, 30)];
    _tf2.backgroundColor = [UIColor whiteColor];
    _tf2.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:_tf2];
    _tf2.delegate = self;



    
    _rightbtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightbtn3.frame = CGRectMake(self.frame.size.width-30, 10+100, 30, 30);
    
    _rightbtn3.layer.cornerRadius = 6;
    _rightbtn3.backgroundColor = [UIColor whiteColor];
    [_rightbtn3 setImage:[UIImage imageNamed:@"Btn_jian"] forState:0];
    [_rightbtn3 addTarget:self action:@selector(BtnClick3:) forControlEvents:UIControlEventTouchUpInside];
    _rightbtn3.tag = 3;
    [self addSubview:_rightbtn3];
    _tf3 = [[UITextField alloc] initWithFrame:CGRectMake(105, 10+100, 180, 30)];
    _tf3.backgroundColor = [UIColor whiteColor];
     _tf3.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:_tf3];
    _tf1.placeholder =@"请输入联系方式";
    _tf2.placeholder =@"请输入联系方式";
    _tf3.placeholder =@"请输入联系方式";
    _tf1.font = [UIFont systemFontOfSize:14];
    _tf2.font = [UIFont systemFontOfSize:14];
    _tf3.font = [UIFont systemFontOfSize:14];

    _tf3.delegate = self;
    if (self.dataarray.count>=3) {
        _rightbtn3.hidden = NO;
        _tf3.hidden = NO;
        _rightbtn2.hidden = NO;
        _tf2.hidden = NO;
        
        _tf1.text =self.dataarray[0];
        _tf2.text =self.dataarray[1];
        _tf3.text =self.dataarray[2];
    }else if (self.dataarray.count==2){
        _rightbtn3.hidden = YES;
        _tf3.hidden = YES;
        _rightbtn2.hidden = NO;
        _tf2.hidden = NO;
        _tf1.text =self.dataarray[0];
        _tf2.text =self.dataarray[1];
    }else{
        _rightbtn3.hidden = YES;
        _tf3.hidden = YES;
        _rightbtn2.hidden = YES;
        _tf2.hidden = YES;
        if (self.dataarray.count==1) {
            _tf1.text =self.dataarray[0];
        }
    }

}
- (void)reloadviewwitharray:(NSMutableArray *)array
{
    [self.dataarray removeAllObjects];
    self.dataarray = [NSMutableArray arrayWithArray:array];
    if (self.dataarray.count>=3) {
        _rightbtn3.hidden = NO;
        _tf3.hidden = NO;
        _rightbtn2.hidden = NO;
        _tf2.hidden = NO;
        
        _tf1.text =self.dataarray[0];
        _tf2.text =self.dataarray[1];
        _tf3.text =self.dataarray[2];
        CGRect frame = self.frame;
        frame.size.height = 150;
        self.frame = frame;
    }else if (self.dataarray.count==2){
        _rightbtn3.hidden = YES;
        _tf3.hidden = YES;
        _tf3.text = @"";
        _rightbtn2.hidden = NO;
        _tf2.hidden = NO;
        _tf1.text =self.dataarray[0];
        _tf2.text =self.dataarray[1];
        CGRect frame = self.frame;
        frame.size.height = 100;
        self.frame = frame;
    }else{
        _rightbtn3.hidden = YES;
        _tf3.hidden = YES;
        _tf3.text = @"";
        _rightbtn2.hidden = YES;
        _tf2.hidden = YES;
        _tf2.text = @"";
        if (self.dataarray.count>0) {
            _tf1.text =self.dataarray[0];
        }
        
        CGRect frame = self.frame;
        frame.size.height = 50;
        self.frame = frame;
    }
    if (self.dataarray.count==3) { // -
        // 第一个图标换成+
   
        [_rightbtn1 setImage:[UIImage imageNamed:@"Btn_jian"] forState:0];
    }else{// +
        [_rightbtn1 setImage:[UIImage imageNamed:@"Btn_jiahao"] forState:0];

    }
    
}
- (void)gotoAddressBookBtnClick1:(UIButton *)btn{
    if (self.dataarray.count==3) { // -
        // 第一个图标换成+
        if (_delegate && [_delegate respondsToSelector:@selector(cellThreeViewBtnClickWithIndex:Type:)]) {
            [_delegate cellThreeViewBtnClickWithIndex:1 Type:2];
        }
        [_rightbtn1 setImage:[UIImage imageNamed:@"Btn_jiahao"] forState:0];
    }else{// +
        if (self.dataarray.count==2) {// 第一个图标换成-
            [_rightbtn1 setImage:[UIImage imageNamed:@"Btn_jian"] forState:0];
            
        }else{
            [_rightbtn1 setImage:[UIImage imageNamed:@"Btn_jiahao"] forState:0];
            
        }
        if (_delegate && [_delegate respondsToSelector:@selector(cellThreeViewBtnClickWithIndex:Type:)]) {
            [_delegate cellThreeViewBtnClickWithIndex:1 Type:1];
        }
        
    }
}

- (void)BtnClick2:(UIButton *)btn{
    [_rightbtn1 setImage:[UIImage imageNamed:@"Btn_jiahao"] forState:0];

    if (_delegate && [_delegate respondsToSelector:@selector(cellThreeViewBtnClickWithIndex:Type:)]) {
        [_delegate cellThreeViewBtnClickWithIndex:2 Type:2];
    }
}

- (void)BtnClick3:(UIButton *)btn{
    [_rightbtn1 setImage:[UIImage imageNamed:@"Btn_jiahao"] forState:0];

    if (_delegate && [_delegate respondsToSelector:@selector(cellThreeViewBtnClickWithIndex:Type:)]) {
        [_delegate cellThreeViewBtnClickWithIndex:3 Type:2];
    }
}
- (void)reloadviewframe
{
    
    [_tf1 setHidden:NO];
    [_rightbtn1 setHidden:NO];
    
    [_tf2 setHidden:NO];
    [_rightbtn2 setHidden:NO];
    
    [_tf3 setHidden:NO];
    [_rightbtn3 setHidden:NO];

    
    CGRect frame = self.frame;
    frame.size.height = 150;
    self.frame = frame;
    
    CGRect frame1 = _rightbtn1.frame;
    frame1.origin.y = 15;
    _rightbtn1.frame = frame1;
    
    CGRect tf1 = _tf1.frame;
    tf1.origin.y = 15;
    _tf1.frame = tf1;
    
    
    CGRect frame2 = _rightbtn2.frame;
    frame2.origin.y = 15+50;
    _rightbtn2.frame = frame2;
    
    CGRect tf2 = _tf2.frame;
    tf2.origin.y = 15+50;
    _tf2.frame = tf2;
    
    CGRect frame3 = _rightbtn3.frame;
    frame3.origin.y = 15+100;
    _rightbtn3.frame = frame3;
    
    CGRect tf3 = _tf3.frame;
    tf3.origin.y = 15+100;
    _tf3.frame = tf3;

    
}

@end
